package top.healthylife.service;/*
 *@Author GaoZeXi
 *@Created time 2020/4/20 2:51
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
public class AuthenticationExample {

    private static AuthenticationManager am = new SimpleAuthenticationManager();

    public static void main(String[] args) throws Exception {

        String name = "11";
        String password = "22";
        try {
            // request就是第一步，使用name和password封装成为的token
            Authentication request = new UsernamePasswordAuthenticationToken(name, password);
            // 将token传递给Authentication进行验证
            Authentication result = am.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);

        } catch (AuthenticationException e) {
            System.out.println("认证失败：" + e.getMessage());
        }
        System.out.println("认证成功，Security context 包含：" + SecurityContextHolder.getContext().getAuthentication());
    }
}

